/*
 * 文件名： WelcomeActivity.java
 * 描    述：首屏
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.bean.AddressBean;
import com.miguo.bean.AdvertBean;
import com.miguo.bean.CommentBean;
import com.miguo.db.AddressDB;
import com.miguo.db.AdvertDB;
import com.miguo.framework.request.FailData;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.framework.utils.ShortCut;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.ui.R;
import com.miguo.utils.BrightnessUtils;
import com.miguo.utils.ClearCache;
import com.miguo.utils.DateUtils;
import com.miguo.utils.FileUtils;
import com.miguo.utils.SignAlarmUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WelcomeActivity extends BaseActivity {

    //private boolean first = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initPrefs();
        initAlarm();
        initUser();
        clearCache();
        StartPage();
    }

    private void initPrefs() {
        if (!AppContext.Prefs().getLock()) {
            // first = true;
            initAddress();
            AppContext.Prefs().setLock(true);
            AppContext.Prefs().setNoImgMode(false);
            AppContext.Prefs().setSysBrightnessMode(
                    BrightnessUtils.getBrightnessMode(this));
            AppContext.Prefs().setSysScreenBrightness(
                    BrightnessUtils.getSysScreenBrightness(this));
            AppContext.Prefs().setStartTime(System.currentTimeMillis());
            AppContext.Prefs().setClearCacheTime(System.currentTimeMillis());
            ShortCut.creatShortCut(WelcomeActivity.this,
                    getString(R.string.app_name), R.drawable.icon);
        }
        AppContext.isNoImg = AppContext.Prefs().getNoImgMode();
        AppContext.isOpenNight = AppContext.Prefs().getNightMode();
    }

    private void initAlarm() {
        SignAlarmUtils.unSignAlarm();
        SignAlarmUtils.signAlarm();
    }

    private void StartPage() {
        // if (first) {
        // AppHelper.StartGuide(this);
        // } else {
        // }
        AdvertDB advertDB = AdvertDB.getInstance(this);
        List<AdvertBean> AdArray = advertDB.findPosition(1);
        if (AdArray != null && AdArray.size() > 0) {
            AdvertBean Advert = AdArray.get(0);
            Advert.setEdit_time(System.currentTimeMillis());
            advertDB.save(Advert);
            AppHelper.StartAdvert(this, Advert);
        } else {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    AppHelper.StartMain(WelcomeActivity.this);
                }
            }, 500);
        }
    }

    /**
     * 初始化用户信息
     *
     * @return
     */
    private void initUser() {

        RequestJsonHandler AjaxCall = new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {
                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    AppContext.UserInfo().setUserInfo(data);
                } else if (json.optInt("status") == 401) {
                    AppContext.UserInfo().Logout();
                }
            }

            @Override
            public void onRequestFail(FailData data) {
                AppContext.UserInfo().Logout();
            }
        };

        if (null != AppContext.Prefs().getUserToken()) {
            AppContext.UserInfo().setToken(AppContext.Prefs().getUserToken());
            AppContext.UserInfo().setIsLogin(true);
            Map<String, String> param = new HashMap<String, String>();
            param.put("token", AppContext.UserInfo().getToken());
            HttpClient.postEncryptData(Url.toRoute(Url.UserCentre.UserInfo, 1), param,
                    AjaxCall);
        }
    }

    private void initAddress() {
        AddressDB addressDB = AddressDB.getInstance(this);
        if (!addressDB.isAddress()) {
            String address = FileUtils.readAssets(this, "address");
            List<AddressBean> addressList = CommentBean.AddressData(address);
            addressDB.saveList(addressList);
        }
    }

    private void clearCache() {
        long CacheTime = AppContext.Prefs().getClearCacheTime();
        if (DateUtils.getOffectDay(System.currentTimeMillis(), CacheTime) > 3) {
            ClearCache.Clear();
            AppContext.Prefs().setClearCacheTime(System.currentTimeMillis());
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
